<?php

namespace app\service;

use app\model\OrderDealChargeback;
use app\model\OrderEnterpriseCertificateBusiness;
use app\model\OrderEnterpriseCompany;
use app\model\OrderEnterpriseExchange;
use app\model\OrderOtherCertificateBusiness;
use app\model\OrderTransactionLog;
use app\model\OrderTransactionRecord;
use app\model\SystemCertificate;
use app\model\SystemSign;
use app\model\SystemTargetLib;
use think\App;
use think\facade\Db;

class Statistics
{
    private $erleiCertificate_id; //二类证书
    private $yileiCertificate_id;//一类证书

    public function __construct()
    {
        $certificateModel = new SystemCertificate();
        //赋值二类证书id
        $this->erleiCertificate_id = $certificateModel->getWhereCertificateIdModel('Ⅱ');
        $this->yileiCertificate_id = $certificateModel->getWhereCertificateIdModel('Ⅰ');
    }
    public function getUidDataPerformanceData($userData,$start='',$end='')
    {
        $systemService = new System();
        $targetLibModel = new SystemTargetLib();
        $systemSignModel = new SystemSign();

        if (empty($start) && empty($end)){
            $start = date('Y-m-d').'00:00:00';
            $end = date('Y-m-d').'23:59:59';
        }

        //获取时间区间签到天数
        $dayNum = $systemSignModel->whereBetweenTime ('create_date',$start,$end)->count ();
        //当天
        $toDay = date('d');

        //总计
        foreach ($userData as &$userDatum) {
            //拼接用户部门
            if ($userDatum['department_id']['id']){
//                $userDatum['department_id'] = '';
                $userDatum['department_id'] = $systemService->getOrgStrName($userDatum['department_id']['id']);
            }

            //一类业绩总计
            $userDatum['ylyjzj'] = $this->getTotal ($start,$end,$userDatum['id'],$this->yileiCertificate_id);

            //二类业绩总计
            $userDatum['elyjzj'] = $this->getTotal ($start,$end,$userDatum['id'],$this->erleiCertificate_id);
            //公司证书
            $userDatum['zszc_gszs'] = $this->getGsYj ($start,$end,$userDatum['id'],1);
            //倒证
            $userDatum['zszc_dz'] = $this->getGsYj ($start,$end,$userDatum['id'],2);

            //总计
            $userDatum['zszctj'] = $userDatum['zszc_dz']+$userDatum['zszc_gszs'];
            //证书办理
            $userDatum['zsbltj'] = $this->getGsYj ($start,$end,$userDatum['id'],3);
            //企业证书
            $userDatum['qyzstj'] = $this->getGsYj ($start,$end,$userDatum['id'],4);
            //退证一类业绩
            $chargeBack = $this->getChargrBack($start,$end,$userDatum['id'],$this->yileiCertificate_id,$this->erleiCertificate_id);
            $userDatum['yl'] = $chargeBack['yl'];
            //退证二类业绩
            $userDatum['el'] = $chargeBack['el'];
            //月总目标
            $userDatum['yzmb'] = $targetLibModel->getValueTargetModel(date('Y-m'),$userDatum['id'],1)?:0;
            //业绩总计
            $userDatum['yjzj'] = $userDatum['qyzstj']+$userDatum['zsbltj']+$userDatum['zszctj'];

            //实际达成
            $userDatum['sjdc'] = $userDatum['yjzj'];

            $moon_day = $targetLibModel->getValueTargetDayModel(date('Y-m'),$userDatum['id'],3,3)?:0;
            if ($moon_day == 0){
                $userDatum['sjjd'] = 0 .'%';
            }else{
                $userDatum['sjjd'] = round  ($dayNum/$moon_day,2)*100 . '%';
            }
            //二类达成进度
            if ($userDatum['yzmb'] == 0){
                $userDatum['eldcjd'] = 0 .'%';
            }else{
                $userDatum['eldcjd'] = round ($userDatum['elyjzj']/$userDatum['yzmb']*100,2). '%';
                if ($userDatum['eldcjd'] > 100){
                    $userDatum['eldcjd'] = 100 . '%';
                }
                if ($userDatum['eldcjd'] < 0){
                    $userDatum['eldcjd'] = 0 . '%';
                }
            }
            //达成进度
            if ($userDatum['yzmb'] == 0){
                $userDatum['dcjd'] = 0 .'%';
            }else{
                $userDatum['dcjd'] = round ($userDatum['sjdc']/$userDatum['yzmb']*100,2). '%';
                if ($userDatum['dcjd'] > 100){
                    $userDatum['dcjd'] = 100 . '%';
                }
                if ($userDatum['dcjd'] < 0){
                    $userDatum['dcjd'] = 0 . '%';
                }
            }
        }
        return $userData;
    }

    /**
     * 获取总业绩
     * @param $moonStart
     * @param $moonEnd
     * @param $uidArr
     * @param string $cerType
     * @return float|int|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getTotal($moonStart, $moonEnd, $uidArr, $cerType='')
    {

        $company = new OrderEnterpriseCompany();
        $exchange = new OrderEnterpriseExchange();
        $other = new OrderOtherCertificateBusiness();
        $business = new OrderEnterpriseCertificateBusiness();
        $orderDealChargebackModel = new OrderDealChargeback();
        $certificateModel = new SystemCertificate();

        //企业证书注册
        $enterData = $company->getSummary($moonStart,$moonEnd,$uidArr,$cerType);

        //企业倒证
        $exchangeData = $exchange->getSummaryModel($moonStart,$moonEnd,$uidArr,$cerType);

        //证书办理
        $otherData = $other->getSummaryModel($moonStart,$moonEnd,$uidArr,$cerType);

        //企业证书
        $businessData = $business->getSummaryModel($moonStart,$moonEnd,$uidArr);

        //查询时间段退单业绩
        $tdPrice = $this->getChargeback($moonStart,$moonEnd,$uidArr,$cerType);
//        $tdPrice = 0;

        //总业绩
        if($cerType && $cerType != $this->erleiCertificate_id){
            $total = $exchangeData['performance_sum']+$otherData['performance_sum']+$enterData; //如果有证书 不统计企业证书业绩
        }else{
            $total = $exchangeData['performance_sum']+$otherData['performance_sum']+$businessData+$enterData;
        }

        //总业绩-退单金额
        return $total+$tdPrice;
    }

    //获取公司证书业绩
    /**
     * @param $startDate
     * @param $endDate
     * @param $idArr
     * @param $type //1查询公司证书 2查询倒证 3查询证书办理 4查询企业证书
     * @return float|int|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getGsYj($startDate,$endDate,$idArr,$type)
    {
        $company = new OrderEnterpriseCompany();
        $exchange = new OrderEnterpriseExchange();
        $other = new OrderOtherCertificateBusiness();
        $business = new OrderEnterpriseCertificateBusiness();
        $orderDealChargebackModel = new OrderDealChargeback();
//        $total= 0;
        //企业证书注册
        if ($type == 1){
            $enterData = $company->getSummary($startDate,$endDate,$idArr);

            //查询时间段退单业绩
            $tdPrice = $this->getTypeChargeback($startDate,$endDate,$idArr,1);

            //证书注册总业绩需要拿单位价/人数-人才证书的收证价格
//            foreach ($enterData as $enterDatum) {
//                $total += $enterDatum['performance_all'];
//            }

            $total = $tdPrice + $enterData;

        }
        if ($type == 2){
            $exchangeData = $exchange->getSummaryModel($startDate,$endDate,$idArr);

            //查询时间段退单业绩
            $tdPrice = $this->getTypeChargeback($startDate,$endDate,$idArr,2);
            $total = $exchangeData['performance_sum']+$tdPrice;
        }
        if ($type == 3){
            //证书办理
            $otherData = $other->getSummaryModel($startDate,$endDate,$idArr);

            //查询时间段退单业绩
            $tdPrice = $this->getTypeChargeback($startDate,$endDate,$idArr,3);

            $total = $otherData['performance_sum']+$tdPrice;

        }

        if ($type == 4){
            //企业证书
            $businessData = $business->getSummaryModel($startDate,$endDate,$idArr);

            //查询时间段退单业绩
            $tdPrice = $this->getTypeChargeback($startDate,$endDate,$idArr,4);
            $total = $businessData+$tdPrice;
        }


        //总业绩-退单金额
        return $total;
    }

    /**
     * 查询退单金额
     */
    public function getChargeback($meekStart,$meekEnd,$uidArr,$cerId='')
    {
        //查询退单金额
        //查询时间段退单业绩
        $orderDealChargebackModel = new OrderDealChargeback();
        $company = new OrderEnterpriseCompany();
        $exchange = new OrderEnterpriseExchange();
        $other = new OrderOtherCertificateBusiness();
        $business = new OrderEnterpriseCertificateBusiness();
        $systemCertificate = new SystemCertificate();
        $certificateModel = new SystemCertificate();

        $tdData = $orderDealChargebackModel->getBetDateTypeModel($meekStart,$meekEnd);
        $tdPrice = 0;
        if ($tdData){
            foreach ($tdData as $item) {
                //通过certificate_code  order_company_id 查询业绩归属人是否等于登陆id
                $comDa = $company->getIdCertificateCodeFindModel($item['order_company_id'],$item['certificate_code']);
                if ($comDa){
//                    print_r ($comDa);die;
                    //如果有证书 先判断证书是否相等
//                    $cid = $certificateModel->getWhereCertificateIdModel($comDa['certificate_leixing']);
                    $cid = $comDa['certificate_id'][0]; //证书类型
                    if ($cerId){
                        if ($cid == $cerId){
                            if(is_array ($uidArr)){
                                //查看业绩归属人有几人
                                $performanceArr = explode (',', $comDa['performance_uid']);
                                $performanceCount = count ($performanceArr);
                                foreach ($performanceArr as $performanceArrItem) {
                                    if (in_array ($performanceArrItem, $uidArr, true)) {
                                        $tdPrice += $item['performance'] / $performanceCount;
                                    }
                                }
                            }else {
                                //查看业绩归属人有几人
                                $performanceArr = explode (',', $comDa['performance_uid']);
                                $performanceCount = count ($performanceArr);
                                foreach ($performanceArr as $performanceArrItem) {
                                    if ($performanceArrItem == $uidArr) {
                                        $tdPrice += $item['performance'] / $performanceCount;
                                    }
                                }
                            }
                        }else{
                            continue;
                        }
                    }else{
                        if(is_array ($uidArr)){
                            //查看业绩归属人有几人
                            $performanceArr = explode (',', $comDa['performance_uid']);
                            $performanceCount = count ($performanceArr);
                            foreach ($performanceArr as $performanceArrItem) {
                                if (in_array ($performanceArrItem, $uidArr, true)) {
                                    $tdPrice += $item['performance'] / $performanceCount;
                                }
                            }
                        }else {
                            //查看业绩归属人有几人
                            $performanceArr = explode (',', $comDa['performance_uid']);
                            $performanceCount = count ($performanceArr);
                            foreach ($performanceArr as $performanceArrItem) {
                                if ($performanceArrItem == $uidArr) {
                                    $tdPrice += $item['performance'] / $performanceCount;
                                }
                            }
                        }
                    }
                }

                //通过certificate_code  order_company_id 查询业绩归属人是否等于登陆id
                $exDa = $exchange->getWhereIdDataSelectModel($item['order_company_id'],$item['certificate_code']);
                if ($exDa) {
                    $cid = $exDa[0]['certificate_leixing']['id'];
                    if ($cerId){
                        if ($cid == $cerId) {
                            if(is_array ($uidArr)){
                                //查看业绩归属人有几人
                                $performanceArr = explode (',', $exDa[0]['performance_uid']);
                                $performanceCount = count ($performanceArr);
                                foreach ($performanceArr as $performanceArrItem) {
                                    if (in_array ($performanceArrItem, $uidArr)) {
                                        $tdPrice += $item['performance'] / $performanceCount;
                                    }
                                }
                            }else {
                                //查看业绩归属人有几人
                                $performanceArr = explode (',', $exDa[0]['performance_uid']);
                                $performanceCount = count ($performanceArr);
                                foreach ($performanceArr as $performanceArrItem) {
                                    if ($performanceArrItem == $uidArr) {
                                        $tdPrice += $item['performance'] / $performanceCount;
                                    }
                                }
                            }
                        }else{
                            continue;
                        }
                    }else{
                        if(is_array ($uidArr)){
                            //查看业绩归属人有几人
                            $performanceArr = explode (',', $exDa[0]['performance_uid']);
                            $performanceCount = count ($performanceArr);
                            foreach ($performanceArr as $performanceArrItem) {
                                if (in_array ($performanceArrItem, $uidArr)) {
                                    $tdPrice += $item['performance'] / $performanceCount;
                                }
                            }
                        }else {
                            //查看业绩归属人有几人
                            $performanceArr = explode (',', $exDa[0]['performance_uid']);
                            $performanceCount = count ($performanceArr);
                            foreach ($performanceArr as $performanceArrItem) {
                                if ($performanceArrItem == $uidArr) {
                                    $tdPrice += $item['performance'] / $performanceCount;
                                }
                            }
                        }
                    }
                }

                //通过certificate_code  order_company_id 查询业绩归属人是否等于登陆id
                $otDa = $other->getWhereIdDataSelectModel($item['order_company_id'],$item['certificate_code']);
                if ($otDa) {
                    $cid = $otDa[0]['certificate_leixing']['id'];
                    if ($cerId){
                        if ($cid == $cerId) {
                            if (is_array ($uidArr)) {
                                //查看业绩归属人有几人
                                $performanceArr = explode (',', $otDa[0]['performance_uid']);
                                $performanceCount = count ($performanceArr);
                                foreach ($performanceArr as $performanceArrItem) {
                                    if (in_array ($performanceArrItem, $uidArr)) {
                                        $tdPrice += $item['performance'] / $performanceCount;
                                    }
                                }
                            } else {
                                //查看业绩归属人有几人
                                $performanceArr = explode (',', $otDa[0]['performance_uid']);
                                $performanceCount = count ($performanceArr);
                                foreach ($performanceArr as $performanceArrItem) {
                                    if ($performanceArrItem == $uidArr) {
                                        $tdPrice += $item['performance'] / $performanceCount;
                                    }
                                }
                            }
                        }else{
                            continue;
                        }

                    }else{
                        if (is_array ($uidArr)) {
                            //查看业绩归属人有几人
                            $performanceArr = explode (',', $otDa[0]['performance_uid']);
                            $performanceCount = count ($performanceArr);
                            foreach ($performanceArr as $performanceArrItem) {
                                if (in_array ($performanceArrItem, $uidArr)) {
                                    $tdPrice += $item['performance'] / $performanceCount;
                                }
                            }
                        } else {
                            //查看业绩归属人有几人
                            $performanceArr = explode (',', $otDa[0]['performance_uid']);
                            $performanceCount = count ($performanceArr);
                            foreach ($performanceArr as $performanceArrItem) {
                                if ($performanceArrItem == $uidArr) {
                                    $tdPrice += $item['performance'] / $performanceCount;
                                }
                            }
                        }
                    }
                }

                //通过certificate_code  order_company_id 查询业绩归属人是否等于登陆id
                $buDa = $business->getWhereIdDataSelectModel($item['order_company_id'],$item['certificate_code']);
                if ($buDa) {
                    if($cerId == $this->erleiCertificate_id || empty($cerId)){
//                        //企业证书没有证书类别
                        if (is_array ($uidArr)) {
                            //查看业绩归属人有几人
                            $performanceArr = explode (',', $buDa[0]['performance_uid']);
                            $performanceCount = count ($performanceArr);
                            foreach ($performanceArr as $performanceArrItem) {
                                if (in_array ($performanceArrItem, $uidArr)) {
                                    $tdPrice += $item['performance'] / $performanceCount;
                                }
                            }

                        } else {
                            //查看业绩归属人有几人
                            $performanceArr = explode (',', $buDa[0]['performance_uid']);
                            $performanceCount = count ($performanceArr);
                            foreach ($performanceArr as $performanceArrItem) {
                                if ($performanceArrItem == $uidArr) {
                                    $tdPrice += $item['performance'] / $performanceCount;
                                }
                            }

                        }
                    }
                }
            }
        }
        return $tdPrice;
    }

    public function getTypeChargeback($meekStart,$meekEnd,$uidArr,$type)
    {
        //查询退单金额
        //查询时间段退单业绩
        $orderDealChargebackModel = new OrderDealChargeback();
        $company = new OrderEnterpriseCompany();
        $exchange = new OrderEnterpriseExchange();
        $other = new OrderOtherCertificateBusiness();
        $business = new OrderEnterpriseCertificateBusiness();
        $systemCertificate = new SystemCertificate();
        $certificateModel = new SystemCertificate();

        $tdData = $orderDealChargebackModel->getBetDateTypeModel($meekStart,$meekEnd);
        $tdPrice = 0;
        if ($tdData){
            foreach ($tdData as $item) {
                switch ($type){
                    case 1:
                        //通过certificate_code  order_company_id 查询业绩归属人是否等于登陆id
                        $performanceUid = $company->getOrderPerformanceUidModel($item['order_company_id'],$item['certificate_code']);
                        if($performanceUid){
                            if(is_array ($uidArr)){
                                //查看业绩归属人有几人
                                $performanceArr = explode (',', $performanceUid);
                                $performanceCount = count ($performanceArr);
                                foreach ($performanceArr as $performanceArrItem) {
                                    if (in_array ($performanceArrItem, $uidArr, true)) {
                                        $tdPrice += round ($item['performance'] / $performanceCount, 2);
                                    }
                                }
                            }else {
                                //查看业绩归属人有几人
                                $performanceArr = explode (',', $performanceUid);
                                $performanceCount = count ($performanceArr);
                                foreach ($performanceArr as $performanceArrItem) {
                                    if ($performanceArrItem == $uidArr) {
                                        $tdPrice += round ($item['performance'] / $performanceCount, 2);
                                    }
                                }
                            }
                        }
                        break;
                    case 2:
                        //通过certificate_code  order_company_id 查询业绩归属人是否等于登陆id
                        $performanceUid = $exchange->getOrderPerformanceUidModel($item['order_company_id'],$item['certificate_code']);
                        if ($performanceUid){
                            if(is_array ($uidArr)){
                                //查看业绩归属人有几人
                                $performanceArr = explode (',', $performanceUid);
                                $performanceCount = count ($performanceArr);
                                foreach ($performanceArr as $performanceArrItem) {
                                    if (in_array ($performanceArrItem, $uidArr, true)) {
                                        $tdPrice += round ($item['performance'] / $performanceCount, 2);
                                    }
                                }
                            }else {
                                //查看业绩归属人有几人
                                $performanceArr = explode (',', $performanceUid);
                                $performanceCount = count ($performanceArr);
                                foreach ($performanceArr as $performanceArrItem) {
                                    if ($performanceArrItem == $uidArr) {
                                        $tdPrice += round ($item['performance'] / $performanceCount, 2);
                                    }
                                }
                            }
                        }
                        break;
                    case 3:
                        //通过certificate_code  order_company_id 查询业绩归属人是否等于登陆id
                        $performanceUid = $other->getOrderPerformanceUidModel($item['order_company_id'],$item['certificate_code']);
                        if ($performanceUid){
                            if (is_array ($uidArr)) {
                                //查看业绩归属人有几人
                                $performanceArr = explode (',', $performanceUid);
                                $performanceCount = count ($performanceArr);
                                foreach ($performanceArr as $performanceArrItem) {
                                    if (in_array ($performanceArrItem, $uidArr, true)) {
                                        $tdPrice += round ($item['performance'] / $performanceCount, 2);
                                    }
                                }
                            } else {
                                //查看业绩归属人有几人
                                $performanceArr = explode (',', $performanceUid);
                                $performanceCount = count ($performanceArr);
                                foreach ($performanceArr as $performanceArrItem) {
                                    if ($performanceArrItem == $uidArr) {
                                        $tdPrice += round ($item['performance'] / $performanceCount, 2);
                                    }
                                }
                            }
                        }
                        break;
                    case 4:
                        //通过certificate_code  order_company_id 查询业绩归属人是否等于登陆id
                        $performanceUid = $business->getOrderPerformanceUidModel($item['order_company_id'],$item['certificate_code']);
                        if ($performanceUid){
                            if (is_array ($uidArr)) {
                                //查看业绩归属人有几人
                                $performanceArr = explode (',', $performanceUid);
                                $performanceCount = count ($performanceArr);
                                foreach ($performanceArr as $performanceArrItem) {
                                    if (in_array ($performanceArrItem, $uidArr, true)) {
                                        $tdPrice += round ($item['performance'] / $performanceCount, 2);
                                    }
                                }

                            } else {
                                //查看业绩归属人有几人
                                $performanceArr = explode (',', $performanceUid);
                                $performanceCount = count ($performanceArr);
                                foreach ($performanceArr as $performanceArrItem) {
                                    if ($performanceArrItem == $uidArr) {
                                        $tdPrice += round ($item['performance'] / $performanceCount, 2);
                                    }
                                }

                            }
                        }
                        break;
                }
            }
        }
        return $tdPrice;
    }

    /**
     * 获取退单退证业绩
     * @param $startDate
     * @param $endDate
     * @param $id
     * @param $ylId
     * @param $elId
     * @return float[]|int[]
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getChargrBack($startDate,$endDate,$id,$ylId,$elId)
    {
        $yl = 0;//一类业绩总和
        $el = 0;//二类业绩总和
        $orderChargeBack = new OrderDealChargeback();
        $company = new OrderEnterpriseCompany();
        $exchange = new OrderEnterpriseExchange();
        $other = new OrderOtherCertificateBusiness();
        $business = new OrderEnterpriseCertificateBusiness();
        //查询时间端内退单内容
        $chargeData = Db::table('order_deal_chargeback')
            ->whereBetweenTime('create_date',$startDate,$endDate)
            ->where('is_complete',1)
            ->whereNull('delete_date')
            ->field('certificate_code,performance')
            ->select ();

        foreach ($chargeData as $chargeDatum) {
            //企业证书就是二类证书
            $businData = $business->getFinanceFieldFindModel ($chargeDatum['certificate_code']);

            if(!empty($businData)) {
                $uidArr = explode (',',$businData['performance_uid']);
                foreach ($uidArr as $uid) {
                    if($uid == $id){
                        $el += $chargeDatum['performance']/count($uidArr);
                    }
                }
            }
            //查询该证书是一类还是二类证书
            $comData = $company->getCertificateCodeFindModel ($chargeDatum['certificate_code']);

            if ($comData && $comData['performance_uid']) {
                //判断证书是一类还是二类
                $uidArr = explode (',',$comData['performance_uid']);

                if ($comData['certificate_id'][0] == $ylId){
                    foreach ($uidArr as $uid) {
                        if($uid == $id){
                            $yl += $chargeDatum['performance']/count($uidArr);
                        }
                    }
                }

                if ($comData['certificate_id'][0] == $elId){
                    foreach ($uidArr as $uid) {
                        if($uid == $id){
                            $el += $chargeDatum['performance']/count($uidArr);
                        }
                    }
                }

            }
            $exData = $exchange->getFinanceFieldFindModel ($chargeDatum['certificate_code']);
            if ($exData){
                //判断证书是一类还是二类
                $uidArr = explode (',',$exData['performance_uid']);
                if ($exData['certificate_leixing']['id'] == $ylId){
                    foreach ($uidArr as $uid) {
                        if($uid == $id){
                            $yl += $chargeDatum['performance']/count($uidArr);
                        }
                    }
                }

                if ($exData['certificate_leixing']['id'] == $elId){
                    foreach ($uidArr as $uid) {
                        if($uid == $id){
                            $el += $chargeDatum['performance']/count($uidArr);
                        }
                    }
                }
            }
            $othData = $other->getFinanceFieldFindModel ($chargeDatum['certificate_code']);
            if ($othData){
                //判断证书是一类还是二类
                $uidArr = $othData['performance_uid'];
                if ($othData['certificate_id'][0] == $ylId){
                    if($uidArr == $id){
                        $yl += $chargeDatum['performance'];
                    }
                }

                if ($othData['certificate_id'][0] == $elId){
                    if($uidArr == $id){
                        $el += $chargeDatum['performance'];
                    }
                }
            }
        }

        return ['yl'=>$yl,'el'=>$el];
    }

    /**
     * 补充收付款流水列表字段
     */
    public function supField($ids)
    {

        if (!$ids) {
            return [];
        }

        $orderTransactionRecordModel = new OrderTransactionRecord();
        $otherCertificateBusinessModel = new OrderOtherCertificateBusiness();
        $orderEnterpriseCertificateBusinessModel = new OrderEnterpriseCertificateBusiness();
        $companyModel = new OrderEnterpriseCompany();
        $exchangeModel = new OrderEnterpriseExchange();
        $orderTransactionLogModel = new OrderTransactionLog();
        $systemService = new System();
        $userModel = new \app\model\User();

        //查询结果为二维数组
        $data = $orderTransactionRecordModel->getIdArrDataModel ($ids);
//        $data = $orderTransactionRecordModel->getIdArrDataModel (554);

        //拼接订单数据
        foreach ($data as &$datum) {
            $result = $otherCertificateBusinessModel->getCerDataPersonModel($datum['certificate_code'],'person_name');
            if (!empty($result)){
                $datum['person_name'] = $result;
                $datum['enterprise_name'] = '';
                $datum['employ_unit_title'] = '';
                $datum['reg_unit'] = '';
            }

            $results = $orderEnterpriseCertificateBusinessModel->getCerFieldDataModel($datum['certificate_code']);
            if (!empty($results)){
                $datum['person_name'] = '';
                if (isset($results['enterprise_name'])){
                    $datum['enterprise_name'] = $results['enterprise_name'];
                }else{
                    $datum['enterprise_name'] = '';
                }
                $datum['employ_unit_title'] = '';
                $datum['reg_unit'] = '';
            }

            $resultss = $companyModel->getCertificateCodeFindModel($datum['certificate_code']);
            if (!empty($resultss)){
                $datum['person_name'] = $resultss['person_name'];
                $datum['enterprise_name'] = '';
                $datum['employ_unit_title'] = $resultss['employ_unit_title'];
                $datum['reg_unit'] = $resultss['reg_unit'];
            }

            $resultsss = $exchangeModel->getFinanceFieldFindModel($datum['certificate_code']);
            if (!empty($resultsss)){
                $datum['person_name'] = $resultsss['person_name'];
                $datum['enterprise_name'] = isset($resultsss['enterprise_name'])?:'';
                $datum['employ_unit_title'] = $resultsss['employ_unit_title'];
                $datum['reg_unit'] = $resultsss['unit_reg_title'];
            }

            //审批时间
            $datum['examine_date'] = $orderTransactionLogModel->getExamineDateModel($datum['id']) ?: '';
            //审批人部门
            $datum['examine_org'] = $systemService->getOrgStrName ($userModel->getUidOrgId ($datum['examine_uid']['id']));

            if (!isset($datum['employ_unit_title'])){
                $datum['employ_unit_title'] = '';
                $datum['reg_unit'] = '';
                $datum['person_name'] = '';
                $datum['enterprise_name'] = '';
            }
        }
        //返回二维数组下标为0，data只包含0
        return $data[0];
    }

    /**
     *  获取证书注册退单全部业绩
     */
    public function getZszcChargeback($meekStart,$meekEnd,$uidArr,$cerId='')
    {
        //查询退单金额
        //查询时间段退单业绩
        $orderDealChargebackModel = new OrderDealChargeback();
        $company = new OrderEnterpriseCompany();
        $exchange = new OrderEnterpriseExchange();
//        $other = new OrderOtherCertificateBusiness();
//        $business = new OrderEnterpriseCertificateBusiness();
//        $systemCertificate = new SystemCertificate();
//        $certificateModel = new SystemCertificate();

        $tdData = $orderDealChargebackModel->getBetDateTypeModel($meekStart,$meekEnd);
        $tdPrice = 0;
        if ($tdData){
            foreach ($tdData as $item) {
                //通过certificate_code  order_company_id 查询业绩归属人是否等于登陆id
                $comDa = $company->getIdCertificateCodeFindModel($item['order_company_id'],$item['certificate_code']);
                if ($comDa){
                    //有没有证书无所谓 计算证书注册总业绩
                    $tdPrice += $item['performance'];
//                    print_r ($comDa);die;
                    //如果有证书 先判断证书是否相等
//                    $cid = $certificateModel->getWhereCertificateIdModel($comDa['certificate_leixing']);
//                    $cid = $comDa['certificate_id'][0]; //证书类型
//                    if ($cerId){
//                        if ($cid == $cerId){
//                            if(is_array ($uidArr)){
//                                //查看业绩归属人有几人
//                                $performanceArr = explode (',', $comDa['performance_uid']);
//                                $performanceCount = count ($performanceArr);
//                                foreach ($performanceArr as $performanceArrItem) {
//                                    if (in_array ($performanceArrItem, $uidArr, true)) {
//                                        $tdPrice += $item['performance'] / $performanceCount;
//                                    }
//                                }
//                            }else {
//                                //查看业绩归属人有几人
//                                $performanceArr = explode (',', $comDa['performance_uid']);
//                                $performanceCount = count ($performanceArr);
//                                foreach ($performanceArr as $performanceArrItem) {
//                                    if ($performanceArrItem == $uidArr) {
//                                        $tdPrice += $item['performance'] / $performanceCount;
//                                    }
//                                }
//                            }
//                        }else{
//                            continue;
//                        }
//                    }else{
//                        if(is_array ($uidArr)){
//                            //查看业绩归属人有几人
//                            $performanceArr = explode (',', $comDa['performance_uid']);
//                            $performanceCount = count ($performanceArr);
//                            foreach ($performanceArr as $performanceArrItem) {
//                                if (in_array ($performanceArrItem, $uidArr, true)) {
//                                    $tdPrice += $item['performance'] / $performanceCount;
//                                }
//                            }
//                        }else {
//                            //查看业绩归属人有几人
//                            $performanceArr = explode (',', $comDa['performance_uid']);
//                            $performanceCount = count ($performanceArr);
//                            foreach ($performanceArr as $performanceArrItem) {
//                                if ($performanceArrItem == $uidArr) {
//                                    $tdPrice += $item['performance'] / $performanceCount;
//                                }
//                            }
//                        }
//                    }
                }

                //通过certificate_code  order_company_id 查询业绩归属人是否等于登陆id
                $exDa = $exchange->getWhereIdDataSelectModel($item['order_company_id'],$item['certificate_code']);
                if ($exDa) {
                    $tdPrice += $item['performance'];
                }
            }
        }
        return $tdPrice;
    }
}